<?php

namespace mbilling\BillingBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;

use mbilling\BillingBundle\Model\DomainsStopModel;
use mbilling\BillingBundle\Model\UsersAdminLogsModel;
use mbilling\BillingBundle\Entity\domainsStop;
use mbilling\BillingBundle\Entity\usersAdminLogs;

use mbilling\BillingBundle\Form\DomainsStopForm;

class DomainsStopController extends Controller
{
    public function indexAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $domainStop_model = new DomainsStopModel($em);
        $domainsStop = $domainStop_model->findAll();
        return $this->render('BillingBundle:Domains_stop:index.html.twig', array('domainsStop' => $domainsStop));
    }
    public function addAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $domainStop_model = new DomainsStopModel($em);
        
        $domainStop = new domainsStop();
        //$domainStop_model->create($domainStop->initDefaultEntity());//создание по умолчанию
        //return new RedirectResponse($this->generateUrl('domains_zones_show'));
        
        $form = $this->createForm(new DomainsStopForm(), $domainStop);
        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $domainStop_model->create($form->getData());
                $securityContext = $this->container->get('security.context');
                $userCurrent = $securityContext->getToken()->getUser();

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('17');//страница стоп-домены
                $log->setObjectId($domainStop->getId());//изменяемый объект
                $log->setActionTypeId('1');//создание стоп-домена
                $log_model->create($log);

                return new RedirectResponse($this->generateUrl('domains_stop_show'));
            }
        }
        return $this->render('BillingBundle:Domains_stop:create.html.twig', array('form' => $form->createView()));
    }
    public function updateIdAction($id)
    {
        $this->get('session')->set('domainStop_id', $id);
        return new RedirectResponse($this->generateUrl('domains_stop_update'));
    }
    public function updateAction()
    {
        $id = $this->get('session')->get('domainStop_id');
        $em = $this->get('doctrine.orm.entity_manager');
        $domainStop_model = new DomainsStopModel($em);
        $domainStop = $domainStop_model->findByPK($id);

        $form = $this->createForm(new DomainsStopForm(), $domainStop);
        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $domainStop_model->create($form->getData());
                $this->get('session')->set('domainStop_id', '');
                $securityContext = $this->container->get('security.context');
                $userCurrent = $securityContext->getToken()->getUser();

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('17');//страница стоп-домены
                $log->setObjectId($domainStop->getId());//изменяемый объект
                $log->setActionTypeId('2');//изменение стоп-домена
                $log_model->create($log);

                return new RedirectResponse($this->generateUrl('domains_stop_show'));
            }
        }
        return $this->render('BillingBundle:Domains_stop:create.html.twig', array('form' => $form->createView()));
    }
    public function removeAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $domainStop_model = new DomainsStopModel($em);

        $domainStop = $domainStop_model->findByPK($id);
        $securityContext = $this->container->get('security.context');
        
        $domainStop_model->removeByPK($id);
        $userCurrent = $securityContext->getToken()->getUser();
        $log_model = new UsersAdminLogsModel($em);
        $log = new usersAdminLogs();
        $log->setAdminId($userCurrent->getId());
        $log->setPageId('17');//страница стоп-домены
        $log->setObjectId($id);//изменяемый объект
        $log->setActionTypeId('3');//удаление стоп-домена
        $log_model->create($log);
            
        return new RedirectResponse($this->generateUrl('domains_stop_show'));
    }
}